package com.legal.aid.system.service.impl;

import java.util.List;

import com.legal.aid.common.core.domain.model.LoginUser;
import com.legal.aid.common.exception.ServiceException;
import com.legal.aid.common.utils.DateUtils;
import com.legal.aid.common.utils.SecurityUtils;
import com.legal.aid.system.mapper.SysCommonUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.legal.aid.common.core.domain.entity.CommonUser;
import com.legal.aid.system.service.ICommonUserService;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-27
 */
@Service
public class CommonUserServiceImpl implements ICommonUserService 
{
    @Autowired
    private SysCommonUserMapper commonUserMapper;

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public CommonUser selectCommonUserById(Long id)
    {
        return commonUserMapper.selectCommonUserById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param commonUser 用户
     * @return 用户
     */
    @Override
    public List<CommonUser> selectCommonUserList(CommonUser commonUser)
    {
        return commonUserMapper.selectCommonUserList(commonUser);
    }

    /**
     * 新增用户
     * 
     * @param commonUser 用户
     * @return 结果
     */
    @Override
    public int insertCommonUser(CommonUser commonUser)
    {

        // 校验用户名是否已存在
        CommonUser commonUserByUsername = commonUserMapper.getCommonUserByUsername(commonUser.getUserName());

        if (commonUserByUsername !=null) {
            throw new ServiceException("该"+commonUserByUsername+"用户存在");
        }
        // 执行插入操作
        LoginUser loginUser = SecurityUtils.getLoginUser();
        commonUser.setCreatedBy(loginUser.getUsername());
        commonUser.setCreateTime(DateUtils.getNowDate());
        return commonUserMapper.insertCommonUser(commonUser);
    }

    /**
     * 修改用户
     * 
     * @param commonUser 用户
     * @return 结果
     */
    @Override
    public int updateCommonUser(CommonUser commonUser)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        commonUser.setUpdatedBy(loginUser.getUsername());
        commonUser.setUpdateTime(DateUtils.getNowDate());
        return commonUserMapper.updateCommonUser(commonUser);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteCommonUserByIds(Long[] ids)
    {
        return commonUserMapper.deleteCommonUserByIds(ids);
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteCommonUserById(Long id)
    {
        return commonUserMapper.deleteCommonUserById(id);
    }
}
